package com.adv.ems.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.adv.ems.db.mapper.EnergyDataProcessMapper;
import com.adv.ems.db.entity.EnergyDataProcessDO;
import com.adv.ems.manager.EnergyDataProcessManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EnergyDataProcessManagerImpl extends ServiceImpl<EnergyDataProcessMapper, EnergyDataProcessDO> implements EnergyDataProcessManager {
    @Override
    public List<EnergyDataProcessDO> getByPointIdAndColDate(Long collectPointId, Date colDate) {
        LambdaQueryWrapper<EnergyDataProcessDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataProcessDO::getCollectPointId, collectPointId);
        wrapper.eq(EnergyDataProcessDO::getColDate, colDate);
        wrapper.orderByDesc(EnergyDataProcessDO::getId);
        return list(wrapper);
    }

    @Override
    public void insertData(EnergyDataProcessDO record) {
        save(record);
    }

    @Override
    public void deleteColData(Long id, Date colDate) {
        LambdaQueryWrapper<EnergyDataProcessDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataProcessDO::getId, id);
        wrapper.eq(EnergyDataProcessDO::getColDate, colDate);
        remove(wrapper);
    }

    @Override
    public List<EnergyDataProcessDO> selectData(Date startDate, Date endDate, List<Long> pointIds) {
        LambdaQueryWrapper<EnergyDataProcessDO> wrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isEmpty(pointIds)) {
            return new ArrayList<>();
        }
        wrapper.in(EnergyDataProcessDO::getCollectPointId, pointIds);
        wrapper.ge(EnergyDataProcessDO::getColDate, startDate);
        wrapper.le(EnergyDataProcessDO::getColDate, endDate);
        wrapper.orderByAsc(EnergyDataProcessDO::getColDate);
        wrapper.last("limit 100000");
        return list(wrapper);
    }
}
